import BaseGroup from '@/components/js/BaseGroup'
import { EventBus } from '@/store/index'

export default class LotteryTurnTable extends BaseGroup{
  constructor() {
    super();
    //子类覆盖
    this.gsap = null;
    this.rotateComps = [];
    // {"arrowCompId":"0f786f9e-a1c3-47fb-94d9-2bc0499e7953","rewardCount":16} 1913
  }
  start() {
    if(!this.item.interactionData) {
      this.rotateComps.push(this.item.id);
    } else {
      const injectJsClassObj = this.item.interactionData.injectJsClassObj;
      const arrowCompId = injectJsClassObj.arrowCompId;
      if(arrowCompId) {
        _.forEach(this.components, (compData) => {
          if(compData.id != arrowCompId) {
            this.rotateComps.push(compData);
          }
        })
      }
    }
    this.onSlotMachine = this.onSlotMachine.bind(this);
    EventBus.$off("slotMachine", this.onSlotMachine);
    EventBus.$on("slotMachine", this.onSlotMachine);
  }

  onSlotMachine() {
    let gsap = window.gsap;
    const injectJsClassObj = this.item.interactionData.injectJsClassObj;

    let circleNum = gsap.utils.random(3, 5, 1);
    let rewardCount = injectJsClassObj.rewardCount;
    //中奖 索引，后端传过来的
    let rewardIndex = 12;
    let degree = 360/rewardCount;
    let rotation = circleNum * 360 + rewardIndex * degree;
    _.forEach(this.rotateComps, (compData) => {
      let dom = document.getElementById(compData.id);
      gsap.to(dom, { duration: circleNum*1, rotation: rotation});
    })
  }

  destroy() {
    super.destroy();
    this.rotateComps = null;
    this.gsap = null;
    EventBus.$off("slotMachine", this.onSlotMachine);
  }
}